/*
 *  (C) Copyright 2007 Insula Tecnologia da Informacao Ltda.
 * 
 *  This file is part of Jazz.
 *
 *  Jazz is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Jazz is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Jazz.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.openinsula.jazz;

import static junit.framework.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.util.LinkedList;
import java.util.List;

import javax.swing.JCheckBox;
import javax.swing.JTextField;

import org.junit.Before;
import org.junit.Test;
import org.openinsula.jazz.binding.BindingAttribute;
import org.openinsula.jazz.binding.BindingProvider;
import org.openinsula.jazz.binding.DefaultBindingAttribute;
import org.springframework.stereotype.Controller;

public class DefaultBindingManagerTestCase {

	private DefaultBindingManager bindingManager;

	private JTextField nameField;

	private JCheckBox activeCheckBox;

	private TestController controller;

	@Before
	public void init() {
		bindingManager = new DefaultBindingManager();
		nameField = new JTextField();
		activeCheckBox = new JCheckBox();
		controller = new TestController();
	}

	@Test
	public void testBindViewToModel() {
		bindingManager.setBindingProvider(new BindingProvider() {
			@Override
			public List<BindingAttribute> getBindingAttributes() {
				List<BindingAttribute> list = new LinkedList<BindingAttribute>();

				try {
					list.add(new DefaultBindingAttribute(DefaultBindingManagerTestCase.this,
							DefaultBindingManagerTestCase.this.getClass().getDeclaredField("nameField"), controller,
							"person.name"));
					list.add(new DefaultBindingAttribute(DefaultBindingManagerTestCase.this,
							DefaultBindingManagerTestCase.this.getClass().getDeclaredField("activeCheckBox"),
							controller, "person.active"));
				}
				catch (Exception ex) {
					throw new RuntimeException(ex);
				}

				return list;
			}
		});

		assertEquals("Yanaga", controller.person.name);
		assertFalse(controller.person.active);
		nameField.setText("Jedi");
		activeCheckBox.setSelected(true);
		bindingManager.bindViewToModel();
		assertEquals("Jedi", controller.person.name);
		assertTrue(controller.person.active);
	}

	@Test
	public void testBindModelToView() {
		bindingManager.setBindingProvider(new BindingProvider() {
			@Override
			public List<BindingAttribute> getBindingAttributes() {
				List<BindingAttribute> list = new LinkedList<BindingAttribute>();

				try {
					list.add(new DefaultBindingAttribute(DefaultBindingManagerTestCase.this,
							DefaultBindingManagerTestCase.this.getClass().getDeclaredField("nameField"), controller,
							"person.name"));
					list.add(new DefaultBindingAttribute(DefaultBindingManagerTestCase.this,
							DefaultBindingManagerTestCase.this.getClass().getDeclaredField("activeCheckBox"),
							controller, "person.active"));
				}
				catch (Exception ex) {
					throw new RuntimeException(ex);
				}

				return list;
			}
		});

		assertEquals("Yanaga", controller.person.name);
		assertFalse(controller.person.active);
		nameField.setText("Jedi");
		activeCheckBox.setSelected(true);
		bindingManager.bindModelToView();
		assertEquals("Yanaga", nameField.getText());
		assertFalse(activeCheckBox.isSelected());
	}

	@Controller
	private class TestController {
		private Person person = new Person();

		public Person getPerson() {
			return person;
		}

		public void setPerson(Person person) {
			this.person = person;
		}

	}

	private class Person {
		private String name = "Yanaga";

		private boolean active = false;

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public boolean isActive() {
			return active;
		}

		public void setActive(boolean active) {
			this.active = active;
		}

	}

}
